package com.touzi.servlets;

import com.touzi.beans.dbs.users.NewUser;
import com.touzi.beans.dbs.users.User;
import com.touzi.beans.dbs.users.UserToUpdate;
import com.touzi.constants.DbCons;
import com.touzi.mapper.UserMapper;
import com.touzi.utils.UsersUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "UsersServlet")
public class UsersServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String method = request.getParameter("method");
        if (method.equals("regnew")) {
            regNew(request, response);
        } else if (method.equals("modifyuser")) {
            modifyUser(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String method = " ";
        method = request.getParameter("method");
        if (method.equals("userlist")) {
            userList(request, response);
        } else if (method.equals("findBydh")) {
            findBydh(request, response);
        } else if (method.equals("tuandui")) {  //团队
            tuanDui(request, response);
        } else if (method.equals("tomodifybyid")) {
            toModifyById(request, response);
        } else {
            request.setAttribute("msg", "没有执行参数");
            request.getRequestDispatcher("/WEB-INF/jsp/msg.jsp").forward(request, response);
        }
    }

    private void toModifyById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        SqlSession sqlSession = ((SqlSessionFactory) getServletContext().getAttribute(DbCons.DB_FACTORY)).openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.findUserByID(Integer.parseInt(id));
        request.setAttribute("user", user);
        request.getRequestDispatcher("WEB-INF/jsp/modifyuser.jsp").forward(request, response);
    }

    private void findBydh(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SqlSession sqlSession = ((SqlSessionFactory) getServletContext().getAttribute(DbCons.DB_FACTORY)).openSession();
        String dianhua = request.getParameter("dianhua");
        List<User> users = sqlSession.getMapper(UserMapper.class).findUserByDianhua(dianhua);
        sqlSession.close();

        request.setAttribute("total", users.size());
        request.setAttribute("userlist", users);
        request.getRequestDispatcher("/WEB-INF/jsp/userlist.jsp").forward(request, response);
    }

    private void userList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SqlSession sqlSession = ((SqlSessionFactory) getServletContext().getAttribute(DbCons.DB_FACTORY)).openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int page = 1;
        try {
            page = Integer.parseInt(request.getParameter("page"));
        } catch (Exception e) {
            e.printStackTrace();
            page = 1;
        }

        int total = mapper.countAll();
        int pages = (int) Math.ceil(total / 30.0);
        if (page < 1) {
            page = 1;
        } else if (page >= pages) {
            page = pages;
        }
        List<User> users = mapper.selectPage((page - 1) * 30);
        sqlSession.close();

        request.setAttribute("userlist", users);
        request.setAttribute("page", page);
        request.setAttribute("pages", pages);
        request.setAttribute("total", total);
        request.getRequestDispatcher("/WEB-INF/jsp/userlist.jsp").forward(request, response);
    }

    private void modifyUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SqlSession sqlSession = ((SqlSessionFactory) getServletContext().getAttribute(DbCons.DB_FACTORY)).openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        UserToUpdate userToUpdate = UsersUtils.getUserToUp(request);
        mapper.updateUserByid(userToUpdate);
        sqlSession.commit();
        sqlSession.close();
        request.setAttribute("msg", "ok");
        request.getRequestDispatcher("/WEB-INF/jsp/msg.jsp").forward(request, response);
    }

    private void tuanDui(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        SqlSession sqlSession = ((SqlSessionFactory) getServletContext().getAttribute(DbCons.DB_FACTORY)).openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        List<User> yiji = mapper.findUserByTuijian(Integer.valueOf(id));

        List<User> erji = new ArrayList<>();
        for (User u : yiji) {
            erji.addAll(mapper.findUserByTuijian(Integer.valueOf(u.id)));
        }

        List<User> sanji = new ArrayList<>();
        for (User u : erji) {
            sanji.addAll(mapper.findUserByTuijian(Integer.valueOf(u.id)));
        }

        sqlSession.close();
        request.setAttribute("userid", id);
        request.setAttribute("yiji", yiji);
        request.setAttribute("erji", erji);
        request.setAttribute("sanji", sanji);
        request.getRequestDispatcher("WEB-INF/jsp/tuijian.jsp").forward(request, response);
    }


    private void regNew(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String dianhua = request.getParameter("dianhua");
        SqlSession sqlSession = ((SqlSessionFactory) getServletContext().getAttribute(DbCons.DB_FACTORY)).openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> u = mapper.findUserByDianhua(dianhua);
        if (u.size() > 0) {
            request.setAttribute("msg", "电话号码重复");
            request.getRequestDispatcher("/WEB-INF/jsp/msg.jsp").forward(request, response);
            return;
        }
        NewUser user = UsersUtils.getNewUserFromRequest(request);
        int i = mapper.regNew(user);
        sqlSession.commit();
        sqlSession.close();
        if (i > 0) {
            request.setAttribute("msg", "注册成功");
            request.getRequestDispatcher("/WEB-INF/jsp/msg.jsp").forward(request, response);
        }
    }
}
